package com.lanou.myapp.person;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lanou.myapp.R;
import com.lanou.myapp.mode.DayNightMode;
import com.lanou.myapp.rains.RaninActivity;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * Created by dllo on 16/1/23.
 */
public class PersonFragment extends Fragment implements View.OnClickListener {
    private Context context;
    private ImageView iv_home, iv_menu, iv_music, iv_share, iv_sun_moon;
    private RelativeLayout level1, level2, level3;
    private String id;
    private String imageUrl;
    private String name;

    private boolean isShowLevel2 = true;// 是否显示2级菜单
    private boolean isShowLevel3 = true;// 是否显示3级菜单

    private boolean isShowMenu = true;// 是否显示整个菜单，包括1级，2级，3级的菜单


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setListener();
        ShareSDK.initSDK(getActivity());

    }


    /**
     * 初始化空间
     */
    private void initView() {
        iv_sun_moon = (ImageView) getActivity().findViewById(R.id.sun_moon);
        iv_share = (ImageView) getActivity().findViewById(R.id.iv_share);
        iv_home = (ImageView) getActivity().findViewById(R.id.iv_home);
        iv_menu = (ImageView) getActivity().findViewById(R.id.iv_menu);
        iv_music = (ImageView) getActivity().findViewById(R.id.iv_music);
        level1 = (RelativeLayout) getActivity().findViewById(R.id.level1);
        level2 = (RelativeLayout) getActivity().findViewById(R.id.level2);
        level3 = (RelativeLayout) getActivity().findViewById(R.id.level3);


    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        iv_home.setOnClickListener(this);
        iv_menu.setOnClickListener(this);
        iv_music.setOnClickListener(this);
        iv_share.setOnClickListener(this);
        iv_sun_moon.setOnClickListener(this);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {

            //当动画都结束的时候才可以操作
            if (AnimUtil.animCount != 0) {
                return true;
            }
            if (isShowMenu) {
                // 需要关闭所有菜单
                int startOffset = 0;
                if (isShowLevel3) {
                    AnimUtil.showOrChoseMenu(level3, startOffset, false);
                    isShowLevel3 = false;
                    startOffset += 200;
                }
                if (isShowLevel2) {
                    AnimUtil.showOrChoseMenu(level2, startOffset, false);
                    isShowLevel2 = false;
                    startOffset += 200;
                }

                AnimUtil.showOrChoseMenu(level1, startOffset, false);

            } else {
                // 需要显示所有菜单
                AnimUtil.showOrChoseMenu(level1, 0, true);
                AnimUtil.showOrChoseMenu(level2, 200, true);
                isShowLevel2 = true;
                AnimUtil.showOrChoseMenu(level3, 400, true);
                isShowLevel3 = true;

            }
            isShowMenu = !isShowMenu;

            return true;
        }
        return super.getActivity().onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_home:
                //当动画都结束的时候才可以操作
                if (AnimUtil.animCount != 0) {
                    // 说明有动画在执行
                    return;
                }
                if (isShowLevel2) {
                    // 需要隐藏
                    int startOffset = 0;
                    if (isShowLevel3) {
                        AnimUtil.showOrChoseMenu(level3, startOffset, false);
                        startOffset += 200;//因为菜单3先隐藏 所以给菜单200秒的延时
                        isShowLevel3 = false;
                    }

                    AnimUtil.showOrChoseMenu(level2, startOffset, false);
                } else {
                    // 需要显示
                    // Log.e(tag, "执行显示操作");
                    AnimUtil.showOrChoseMenu(level2, 0, true);
                }
                isShowLevel2 = !isShowLevel2;
                break;
            case R.id.iv_menu:
                //当动画都结束的时候才可以操作
                if (AnimUtil.animCount != 0) {
                    // 说明有动画在执行
                    return;
                }
                if (isShowLevel3) {
                    // 隐藏3级菜单
                    AnimUtil.showOrChoseMenu(level3, 0, false);
                } else {
                    // 显示3级菜单
                    AnimUtil.showOrChoseMenu(level3, 0, true);
                }
                isShowLevel3 = !isShowLevel3;
                break;

            case R.id.iv_share:
                showShare();
                break;

            case R.id.iv_music:
                //微博登录
                PlatformActionListener listener = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        //授权成功
                        id = platform.getDb().getUserId();
                        name = platform.getDb().getUserName();
                        imageUrl = platform.getDb().getUserIcon();
                        Log.d("message", "id:" + id + "name:" + name + "url:" + imageUrl);
                        Log.d("hashmap=======", "" + hashMap);
                        Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        //授权失败
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        //取消授权
                    }
                };
                Platform sina = ShareSDK.getPlatform(getActivity(), SinaWeibo.NAME);

                sina.setPlatformActionListener(listener);
                sina.showUser(null);
                sina.authorize();
                break;

            case R.id.sun_moon:
                int theme = DayNightMode.getDayNightMode(getActivity());
                if (theme == DayNightMode.THEME_SUN) {
                    Toast.makeText(getActivity(), "夜间模式", Toast.LENGTH_SHORT).show();
                    DayNightMode.setDayNightMode(getActivity(), DayNightMode.THEME_NIGHT);
                } else {
                    Toast.makeText(getActivity(), "白天模式", Toast.LENGTH_SHORT).show();
                    DayNightMode.setDayNightMode(getActivity(), DayNightMode.THEME_SUN);
                }
                // 注意改过主题后一定要，把activity finish在重开一遍，因为更改主题只能在oncreat中进行
                getActivity().finish();
                getActivity().startActivity(new Intent(getActivity(), getActivity().getClass()));
                break;
            default:
                break;
        }
    }


    private void showShare() {
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("分享");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(getActivity());
    }





    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_person, null);
        return view;
    }
}
